<template>
	<el-dialog :title="form.id ? '编辑' : '新增配置'" v-model="visible" :close-on-click-modal="false" draggable width="560px">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="单位名称" prop="deptId">
						<el-tree-select
							v-model="form.deptId"
							:data="parentData"
							:props="{ value: 'id', label: 'name', children: 'children', disabled: 'isLock' }"
							clearable
							filterable
							check-strictly
							:render-after-expand="false"
							placeholder="请选择所属单位"
							:teleported="false"
						/>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="平台年限" prop="addPlatformFeeYears">
						<el-input-number :min="0" :max="999" :precision="0" :value-on-clear="0" v-model="form.addPlatformFeeYears" placeholder="请输入平台年限" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="设备数量" prop="deviceAddNum">
						<el-input-number
							:min="0"
							:max="99999"
							:precision="0"
							:value-on-clear="0"
							v-model="form.deviceAddNum"
							placeholder="请输入设备数量（台）"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="语音总分钟数" prop="ttsAddNum">
						<el-input-number
							:min="0"
							:max="99999"
							:precision="0"
							:value-on-clear="0"
							v-model="form.ttsAddNum"
							placeholder="请输入语音总分钟数（分钟）"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="短信总数" prop="smsAddNum">
						<el-input-number :min="0" :max="99999" :precision="0" :value-on-clear="0" v-model="form.smsAddNum" placeholder="请输入短信总数（条）" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicExpensesDeptDetailDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/tariff/basicExpensesDeptDetail';
import { rule } from '/@/utils/validate';
import { deptTree } from '/@/api/admin/dept';
import { useUserInfo } from '/@/stores/userInfo';
import { findNode } from '/@/utils/treeHelper';
const emit = defineEmits(['refresh']);
const userInfo = useUserInfo();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const parentData = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	deptId: '',
	addPlatformFeeYears: null,
	smsAddNum: null,
	ttsAddNum: null,
	deviceAddNum: null,
	deliveryMethod: 10,
});

// 定义校验规则
const dataRules = ref({
	deptId: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
	addPlatformFeeYears: [{ required: true, message: '平台年限不能为空', trigger: 'blur' }],
	smsAddNum: [{ required: true, message: '新增短信数（条）不能为空', trigger: 'blur' }],
	ttsAddNum: [{ required: true, message: '新增语音数（分钟）不能为空', trigger: 'blur' }],
	deviceAddNum: [{ required: true, message: '新增设备数（台）不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	getDeptData();

	// 获取basicExpensesDeptDetail信息
	if (id) {
		form.id = id;
		getbasicExpensesDeptDetailData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicExpensesDeptDetailData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

const getDeptData = async () => {
	deptTree().then((res) => {
		const deptId = userInfo.userInfos?.user?.deptId;

		if (deptId) {
			const nodes = findNode(res.data, (node: any) => {
				return node.id === deptId;
			});
			parentData.value = nodes?.children.map((d: any) => {
				delete d['children'];
				return d;
			});
		} else {
			parentData.value = [];
		}
	});
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
